package com.library.cxq.servlet;

import com.alibaba.fastjson.JSON;
import com.library.cx.dao.Libraryimpl;
import com.library.cx.into.Libary;
import com.library.cx.service.Totalimpl;
import com.library.cx.util.Page;
import com.library.cxq.service.impl.ReaderServiceImpl;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

public class ReaderLogin extends HttpServlet {
    ReaderServiceImpl readerService=new ReaderServiceImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=UTF-8");

        String tp=req.getParameter("tp");
        System.out.println(tp+"tp");
        if(tp.equals("selectBook")){
            String bookname=req.getParameter("bookname");
            String author=req.getParameter("author");
            List<Libary> list = readerService.selectBook(bookname, author);
            req.setAttribute("list",list);
            req.getRequestDispatcher("cxq/select.jsp").forward(req,resp);
        }
        String select =req.getParameter("submit");
        if(tp.equals("login")){
        PrintWriter out=resp.getWriter();
        String reaId = req.getParameter("Username");
        String pwd = req.getParameter("Password");
        boolean login = readerService.login(Integer.parseInt(reaId), pwd);
        if(login){
            req.getSession().setAttribute("id",reaId);
            req.getRequestDispatcher("cxq/mainPage.jsp").forward(req,resp);
        }else {
            resp.sendRedirect("cxq/logins.jsp");
        }

        }else if(tp.equals("selectSfz")){
            String phone=req.getParameter("phone");
//            System.out.println(phone+"电话");
            String sfz=req.getParameter("sfz");
                if(sfz!=null){
                    boolean boo = readerService.verification(phone, sfz);
                    PrintWriter out=resp.getWriter();
                    out.print(boo);
                    out.flush();
                    out.close();
                }
            }else if (tp.equals("updatePwd")){
            String sfz=req.getParameter("sfz");
            String id=req.getParameter("id");
            String pwd=req.getParameter("pwd");
            if (sfz==null){
                Integer idd=Integer.parseInt(id);
                readerService.updatePwd(idd,pwd);
                PrintWriter out=resp.getWriter();
                out.print(true);
                out.flush();
                out.close();
            }
            readerService.updatePwd(sfz,pwd);
            req.getRequestDispatcher("cxq/logins.jsp").forward(req,resp);
        }else if(tp.equals("register")){
            PrintWriter out=resp.getWriter();
            Integer reaId=(int)((Math.random()*9+1)*100000);
            String idCard = req.getParameter("idCard");
            String pwd ="123456";//初始密码
            String name = req.getParameter("name");
            Integer age = Integer.parseInt(req.getParameter("age"));
            String phone = req.getParameter("phone");
            double price=0;//初始金额
            readerService.register(reaId, idCard, pwd, name, age, phone,price);
            String isregister = JSON.toJSONString(reaId);
            out.print(isregister);
            out.flush();
            out.close();
        } else if(select!=null){
            Totalimpl totalimpl = new Totalimpl();
            //判断是否成功
            String bookname=req.getParameter("bookname");
            String author=req.getParameter("author");
            String currentPage = req.getParameter("currentPage");
            String deng =req.getParameter("denglu");
            int countss=Integer.parseInt(currentPage);
            String names=req.getParameter("name");//用户名
            Page<Libary> finBay=totalimpl.findByPage(bookname,author,countss);
            req.setAttribute("bays",finBay);
            req.setAttribute("deng",names);
            req.getRequestDispatcher("cx/Administrator.jsp").forward(req,resp);
        }

    }
}
;